<?php

include_once dirname(__FILE__)."/../database/database.class.php";
include_once dirname(__FILE__)."/../database/SQLgenerator.class.php";

class Card_database
{
  public function getCard($cardid)
  {
    global $db;

    $cardid = $db->escape_string($cardid);
    $limit = SQLgenerator::createPageSQL(array('page' => 0, 'pagelimit' => 1000));

    $SQL = "SELECT `Card`.`ID`, `Card_details`.`name`, `Card_details`.`cardside`, `Card`.`expansion`
                            FROM `Card`
                            LEFT JOIN `Card_details`
                            ON `Card`.`ID` = `Card_details`.`card_ID`
                            WHERE `Card`.`ID`=\"$cardid\"
                            $limit";
    return $db->getArray($SQL);
  }

  public function getCardDetails($cardid)
  {
    global $db;

    $cardid = $db->escape_string($cardid);

    $SQL = "SELECT `Card_details`.`name`, `Card_details`.`cardside`, `Card_details`.`Mana_Cost`, `Card_details`.`Converted_Mana_Cost`, `Card_details`.`Types`, `Card_details`.`Flavor_text`, `Expansion`.`name` as expansion_name, `Card_details`.`Rarity`, `Card_details`.`power`, `Card_details`.`toughness`, `Card_details`.`Loyalty`, `Artist`.`name` as artist_name, `Card`.`cardnumber`
            FROM `Card`
            LEFT JOIN `Card_details` ON `Card`.`ID` = `Card_details`.`card_ID`
            LEFT JOIN `Artist` ON `Card_details`.`artist_ID` = `Artist`.`ID`
            JOIN `Expansion` ON `Card`.`expansion` = `Expansion`.`ID`
            WHERE `Card`.`ID`=\"$cardid\"
            ORDER BY `Card_details`.`cardside`";

    $array = $db->getArray($SQL);
    if(!empty($array))
    {
      return $array;
    }
    return 0;
  }

  public function getCardText($cardid)
  {
    global $db;

    $cardid = $db->escape_string($cardid);

    $array = $db->getArray("SELECT `Card_text`.`text` as cardtext, `card_details_text`.`cardside`, `card_details_text`.`amount` FROM `Card`
                        JOIN `card_details_text` ON `Card`.`ID` = `card_details_text`.`card_ID`
                        JOIN `Card_text` ON `card_details_text`.`card_text_ID` = `Card_text`.`card_text_ID`
                        WHERE `Card`.`ID`=$cardid
                        ORDER BY `card_details_text`.`cardside` ASC");
    $text = array();
    foreach($array as $text_item)
    {
      array_push($text, array($text_item->cardtext, $text_item->amount, $text_item->cardside));
    }
    return $text;
  }

  public function getTotalCards()
  {
    global $db;

    $array = $db->getArray("SELECT count(*) as size FROM `Card`");
    if(!empty($array))
    {
      return $array[0]->size;
    }
    return 0;
  }

  public function getXthCard($position)
  {
    global $db;

    $position = $db->escape_string($position);
    $limit = SQLgenerator::createPageSQL(array('page' => $position, 'pagelimit' => 1));

    $array = $db->getArray("SELECT `Card`.`ID` FROM `Card` $limit");
    if(!empty($array))
    {
      return $array[0]->ID;
    }
    return 0;
  }

  public function getUniqueCardByNameAndExpansion($cardname, $cardexpansion)
  {
    global $db;

    $cardname = $db->escape_string($cardname);
    $cardexpansion = $db->escape_string($cardexpansion);
    $limit = SQLgenerator::createPageSQL(array('page' => 0, 'pagelimit' => 1));

    $SQL = "SELECT `Card`.`ID`, `Card_details`.`cardside`
            FROM `Card`
            INNER JOIN `Card_details`
            ON `Card`.`ID` = `Card_details`.`card_ID`
            WHERE `Card_details`.`name`=\"$cardname\" AND `Card`.`expansion`=\"$cardexpansion\" $limit";
    $array = $db->getArray($SQL);
    if(!empty($array))
    {
      return $array[0];
    }
    return 0;
  }

  public function getUniqueCardsByNameAndExpansion($cardname, $cardexpansion)
  {
    global $db;

    $cardname = $db->escape_string($cardname);
    $cardexpansion = $db->escape_string($cardexpansion);

    $SQL = "SELECT `Card`.`ID`
            FROM `Card`
            INNER JOIN `Card_details`
            ON `Card`.`ID` = `Card_details`.`card_ID`
            WHERE `Card_details`.`name`=\"$cardname\" AND `Card`.`expansion`=$cardexpansion";
    
    return $db->getArray($SQL);
  }

  public function getMultiverseID($cardid, $side = 1)
  {
    global $db;

    $cardid = $db->escape_string($cardid);
    $limit = SQLgenerator::createPageSQL(array('page' => 0, 'pagelimit' => 1));

    $multiverseid = $db->getArray("SELECT `multiverse_ID` FROM `card_externalid` WHERE `card_ID`=\"$cardid\" AND `cardside`=\"$side\" $limit");
    if(!empty($multiverseid))
    {
      return $multiverseid[0]->multiverse_ID;
    }
    return 0;
  }

}

$cdb = new Card_database();

?>
